package com.cdd.controller;

import org.jasig.cas.client.authentication.AttributePrincipal;
import org.jasig.cas.client.validation.Assertion;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import static org.jasig.cas.client.util.AbstractCasFilter.CONST_CAS_ASSERTION;

@Controller
public class IndexController {

    /**
     * 访问该接口，返回首页。
     * 集成了thymeleaf框架，则访问ip+端口，也能访问到首页
     * @return
     */
    @RequestMapping("/crm")
    public ModelAndView root(HttpSession session){
        ModelAndView mav=new ModelAndView();
        mav.setViewName("index");
        return mav;
    }

    /**
     * 注销
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.invalidate();
        return "redirect:https://cdd1234.com:8443/cas/logout";
    }

    /**
     * 后台获取用户名称
     * @param session
     * @return
     */
    @RequestMapping("/test")
    @ResponseBody
    public String test1(HttpSession session){
        Assertion assertion = (Assertion)session.getAttribute(CONST_CAS_ASSERTION);
        AttributePrincipal principal = assertion.getPrincipal();
        String loginName = principal.getName();
        return "sso-test222222，当前登录账户"+loginName;
    }
}
